package com.ruix.app.ui.usercenter;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.constant.RuiXConstant;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.UserMapper;
import com.ruix.app.model.User;
import com.ruix.app.model.UserInfo;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.base.BaseActivity;
import com.ruix.app.util.SoftInputUtil;
import com.ruix.app.widget.CommonTitleBar;
import com.ruix.app.widget.dialog.CommonDialog;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/5/5.
 */

public class ModifyUserConfigActivity extends BaseActivity implements View.OnClickListener{


    public static Intent newIntent(Context context,int type){
        Intent intent = new Intent(context,ModifyUserConfigActivity.class);
        intent.putExtra("ModifyType",type);
        return intent;
    }

    public static int TypeName = 1;
    public static int TypeLocation = 2;
    public static int TypePosition = 3;
    public static int TypeCompany = 4;
    public static int TypeEmail = 5;

    private int modifyType;
    private ImageView clearView;
    private EditText imputEdit;
    private String tmpString;




    @Override
    protected int loadViewLayout() {
        return R.layout.activity_modify_userconfig;
    }

    @Override
    protected void initView() {
        imputEdit = (EditText)findViewById(R.id.et_imput);
        clearView = (ImageView) findViewById(R.id.iv_clear_imput);

        //
        CommonTitleBar titleBar = getTitleBar();
        TextView titleBarRightBtn = titleBar.getRightBtn();
        titleBarRightBtn.setText(" 保存 ");
        titleBarRightBtn.setTextColor(getResources().getColor(R.color.color_33));
        titleBarRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveUerInfo();
            }
        });

        if (titleBar != null){
            titleBar.setOnClickLeftListener(new CommonTitleBar.OnTitleBarClickLeftListener() {
                @Override
                public void onClickLeftBackBtn() {
                    checkInput();
                }
            });
        }
    }

    //
    private void checkInput(){
        String inputString =  imputEdit.getText().toString().trim();
        if (!TextUtils.isEmpty(inputString) && !inputString.equals(tmpString) ){
            if (modifyType == TypeName) {
                exitTipDialog("是否保存姓名?");
            } else {
                exitTipDialog(null);
            }

        } else {
            finish();
        }
    }

    private void exitTipDialog(String tip){
        CommonDialog dialog = new CommonDialog(this);
        dialog.setListener(new CommonDialog.CommonDialogAcitonListener() {
            @Override
            public void positiveAction() {
                finish();
            }

            @Override
            public void negertivieAction() {

            }
        });
        if (!TextUtils.isEmpty(tip)){
            dialog.show(tip);//
        } else {
            dialog.show("是否保存？");//
        }
        dialog.setPositiveString("是");
        dialog.seCancelString("否");

    }


    @Override
    protected void initLocalData() {
       Intent intent =  getIntent();
        if (intent != null) {
            modifyType = intent.getIntExtra("ModifyType", 0);
        }
        User user = UserManager.getCurrentUser();
        if (user != null) {
            if (modifyType == TypeName) {
                imputEdit.setText(user.getName());
                setTitle("姓名");
            } else if (modifyType == TypeLocation) {
                imputEdit.setText(user.getLocate());
//            setTitle("姓名");
            } else if (modifyType == TypePosition) {
                imputEdit.setText(user.getPosition());
                setTitle("职位");
            } else if (modifyType == TypeCompany) {
                imputEdit.setText(user.getCompany());
                setTitle("公司");
            } else if (modifyType == TypeEmail) {
                imputEdit.setText(user.getEmail());
                setTitle("邮箱");
            }
            imputEdit.setSelection(imputEdit.getText().length());
            imputEdit.setFocusable(true);
            SoftInputUtil.showSoftInput(this,imputEdit);
            tmpString = imputEdit.getText().toString();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void initListener() {
        imputEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() >0){
                    clearView.setVisibility(View.VISIBLE);
                } else {
                    clearView.setVisibility(View.GONE);
                }

            }
        });
        clearView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_clear_imput:
                imputEdit.setText("");
                break;
        }

    }

    private void saveUerInfo(){
        String inputString =  imputEdit.getText().toString().trim();
        if (TextUtils.isEmpty(inputString)){
            if (modifyType == TypeName) {
                showErrorTip("请输入姓名");
            }else if (modifyType == TypePosition) {
                showErrorTip("请输入职位信息");
            } else if (modifyType == TypeCompany) {
                showErrorTip("请输入公司名称");
            } else if (modifyType == TypeEmail) {
                showErrorTip("请输入邮箱地址");
            } else {
                showErrorTip("请输入内容");
            }
            return;
        }

        UserInfo userInfo = new UserInfo();
        if (modifyType == TypeName){
            userInfo.setName(inputString);
        } else if (modifyType == TypeLocation){
            userInfo.setLocate(inputString);
        } else if (modifyType == TypePosition){
            userInfo.setPosition(inputString);
        } else if (modifyType == TypeCompany){
            userInfo.setCompany(inputString);
        } else if (modifyType == TypeEmail){
            userInfo.setEmail(inputString);
        }

        showLoading();
        RequestFactory.editUserinfo(userInfo,new UserMapper(), new RXObserver<User>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(User value) {
                if (value != null && !TextUtils.isEmpty(value.getID())){
                    UserManager.sharedInstance().setCurrentUser(value);
                    UserManager.sharedInstance().saveCurrentUser();
                    if (modifyType == TypeName){
                        sendBroadcast(new Intent(RuiXConstant.NOTICE_CHANGE_USER_INFO));
                    }
                    showSuccessTip("保存成功");
                }

            }

            @Override
            public void onError(Throwable e) {
                dismissLoading();
                resolveException(e);
            }

            @Override
            public void onComplete() {

                dismissLoading();
                finish();
            }
        });

    }

    @Override
    public void onBackPressed() {
        checkInput();
    }
}
